package com.sigmatrix.h5.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.dao.EnterpriseDao;
import com.sigmatrix.h5.dto.EnterpriseData;
import com.sigmatrix.h5.entity.Enterprise;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.service.EnterpriseService;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.web.vo.EnterpriseNomal;
import com.sigmatrix.h5.web.vo.EnterpriseVo;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;

/**
 * 
 * @ClassName: EnterpriseServiceImpl
 * @Description: 企业
 * @author MH
 * @date 2017年11月10日 下午7:22:17
 */
@Service
public class EnterpriseServiceImpl extends ServiceImpl<Enterprise, Integer> implements EnterpriseService {

	/** logger record */
	private static final Logger LOGGER = LoggerFactory.getLogger(EnterpriseServiceImpl.class);

	private ConcurrentHashMap<String, EnterpriseNomal> enterpriseMap = new ConcurrentHashMap<>();

	@Autowired
	private EnterpriseDao enterpriseDao;

	@Override
	public Enterprise findByBasicModuleId(Integer basicModuleId) {
		Enterprise en = new Enterprise();
		en.setBasicModuleId(basicModuleId);
		return enterpriseDao.selectOne(en);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @Title: findById
	 * @Description: 
	 * @see com.sigmatrix.sc.production.mgt.service.EnterpriseService#findById(java.lang.Integer)
	 * @author youjun
	 * @date 2017年8月3日 上午11:08:59
	 */
	@Override
	public Enterprise findById(final Integer id) {
		return enterpriseDao.findById(id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @Title: findListByCondition
	 * @Description: 
	 * @see com.sigmatrix.sc.production.mgt.service.EnterpriseService#findListByCondition(com.sigmatrix.sc.production.mgt.web.vo.EnterpriseVo)
	 * @author youjun
	 * @date 2017年8月3日 上午11:08:54
	 */
	@Override
	public List<EnterpriseData> findListByCondition(final EnterpriseVo enterpriseVo) throws ServiceException {

		final List<EnterpriseData> dataList = new ArrayList<EnterpriseData>();
		try {
			final Enterprise enterprise = new Enterprise();
			if (enterpriseVo != null) {
				BeanUtils.copyProperties(enterpriseVo, enterprise);
			}
			final List<Enterprise> list = enterpriseDao.findListByCondition(enterprise);
			if (!CollectionUtils.isEmpty(list)) {
				EnterpriseData data = null;
				for (final Enterprise item : list) {
					data = new EnterpriseData();
					BeanUtils.copyProperties(item, data);
					dataList.add(data);
				}
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return dataList;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @Title: findByEnterpriseCode
	 * @Description: 
	 * @see com.sigmatrix.sc.handheld.inter.service.EnterpriseService#findByEnterpriseCode(java.lang.String)
	 * @author youjun
	 * @date 2017年8月4日 下午2:15:11
	 */
	@Override
	public EnterpriseData findByEnterpriseCode(final String enterpriseCode) throws ServiceException {

		if (StringUtils.isBlank(enterpriseCode)) {
			throw new ServiceException("企业code不能为空");
		}
		final Enterprise enterprise = new Enterprise();
		enterprise.setCode(enterpriseCode);
		try {
			final List<Enterprise> list = enterpriseDao.findListByCondition(enterprise);
			if (!CollectionUtils.isEmpty(list)) {
				if (list.size() > 1) {
					final StringBuilder errorMsg = new StringBuilder("当前企业code:");
					errorMsg.append(enterpriseCode).append("存在多条记录");
					LOGGER.error(errorMsg.toString());
					LOGGER.error("多条企业数据为:{}", JSONObject.toJSONString(list));
					throw new ServiceException(errorMsg.toString());
				}
				final EnterpriseData enterpriseData = new EnterpriseData();
				BeanUtils.copyProperties(list.get(0), enterpriseData);
				return enterpriseData;
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		return null;
	}

	@Override
	public EnterpriseNomal findByNomalByCode(String enterpriseCode) {
		EnterpriseNomal enterpriseNomal = null;
		if (StringUtils.isBlank(enterpriseCode)) {
			throw new MessageException("企业code不能为空");
		}
		enterpriseNomal = enterpriseMap.get(enterpriseCode);
		if (CommonUtils.isNotEmpty(enterpriseNomal)) {
			return enterpriseNomal;
		} else {
			final Enterprise enterprise = new Enterprise();
			enterprise.setCode(enterpriseCode);
			try {
				final List<Enterprise> list = enterpriseDao.findListByCondition(enterprise);
				if (!CollectionUtils.isEmpty(list)) {
					if (list.size() > 1) {
						final StringBuilder errorMsg = new StringBuilder("当前企业code:");
						errorMsg.append(enterpriseCode).append("存在多条记录");
						LOGGER.error(errorMsg.toString());
						LOGGER.error("多条企业数据为:{}", JSONObject.toJSONString(list));
						throw new MessageException(errorMsg.toString());
					}
					enterpriseNomal = new EnterpriseNomal();
					BeanUtils.copyProperties(list.get(0), enterpriseNomal);
					enterpriseMap.put(enterpriseCode, enterpriseNomal);
					return enterpriseNomal;
				} else {
					throw new MessageException("未查询到企业,企业code为：" + enterpriseCode);
				}
			} catch (DaoException e) {
				LOGGER.error("数据查询异常", e);
				throw new MessageException("企业数据查询异常");
			}
		}
	}

}
